package loader.servelet;

import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import loader.TimerLoader;

public class LoaderServlet extends HttpServlet {

	private static final long serialVersionUID = -3753078970161859067L;

	private Timer timer = null;

	/**
	 * Constructor of the object.
	 */
	public LoaderServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		this.timer.cancel();
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {

		String sdelay = this.getServletContext().getInitParameter("delay");
		String scities = this.getServletContext().getInitParameter("cities");
		final int delay = Integer.parseInt(sdelay);
		final String[] cities = scities.split(",");

		timer = new Timer(true);
		timer.schedule(new TimerTask() {
			public void run() {
				TimerLoader loader = new TimerLoader(cities);
				loader.load();
			}
		}, 0,delay);// milliseconds
	}
}
